package cn.vc.content.controller.article;

import cn.vc.auth.entity.po.VUser;
import cn.vc.basicmodule.entity.Result;
import cn.vc.content.entity.article.Article;
import cn.vc.content.entity.article.ArticleHistory;
import cn.vc.content.entity.video.VideoHistory;
import cn.vc.content.service.article.ArticleHistoryService;
import cn.vc.content.service.article.ArticleService;
import cn.vc.content.util.SecurityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * 文章历史接口
 */
@RestController
@RequestMapping("/articlehistory")
//@PreAuthorize("hasAnyAuthority('ordinary')")
public class ArticleHistoryController {

    @Autowired
    private ArticleService articleService;
    @Resource
    private ArticleHistoryService articleHistoryService;

    /**
     * 查询文章历史记录
     * @return
     */
    @GetMapping
    public Result<List<Article>> selectMyVideoHistory() {
        VUser myUser = SecurityUtil.getUser();
        if (myUser==null) return Result.NOT_LOGGED_IN();
        ArticleHistory articleHistory = articleHistoryService.selectMyArticleHistory(myUser.getId());
        if (articleHistory==null) {
            return new Result<>(1, null, "查询成功");
        } else {
            Collections.reverse(articleHistory.getArticleIdList());
            return new Result<>(1, articleService.selectByIds(articleHistory.getArticleIdList()), "查询成功！");
        }
    }
}
